<template>
  <div class="register-container">
    <el-form class="register-form" :model="user" ref="userRef" label-width="120px" :rules="registerRules" label-position="top">
      <el-form-item label="用户名" prop="userName">
        <el-input min="10" max="16" v-model="user.userName" name="userName" />
      </el-form-item>
      <el-form-item label="昵称" prop="nikeName">
        <el-input min="10" max="16" v-model="user.userInfo.nikeName" name="nikeName" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input max="30" v-model="user.email" name="email"/>
      </el-form-item>
      <el-form-item label="联系方式" prop="tel">
        <el-input type="number"  min="11" max="11" v-model="user.tel" name="tel"/>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input  type="password" max="30" v-model="user.password" name="password"/>
      </el-form-item>
      <el-form-item label="再次输入密码" prop="secPassword">
        <el-input type="password" max="30" v-model="user.secPassword"  name="secPassword"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">注册</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { validatePassword } from './rules'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import router from '@/router'
const user = ref({
  userName: '',
  password: '',
  secPassword: '',
  email: '',
  tel: '',
  userInfo: {
    name: '',
    nikeName: '',
    age: 0,
    sex: '',
    birthDate: new Date()
  }
})

const userRef = ref(user.value)

const i18n = useI18n()

const registerRules = ref({
  userName: [
    {
      required: true,
      trigger: 'blur',
      message: '用户名不能为空'
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      validator: validatePassword()
    }
  ],
  secPassword: [
    {
      required: true,
      trigger: 'blur',
      validator: (rule, value, callback, source, options) => {
        if (!user.value.password) {
          callback(new Error('密码不能为空'))
        } else if (value !== user.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      }
    }
  ],
  email: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.register.email')
    },
    {
      type: 'email',
      trigger: 'blur',
      message: i18n.t('msg.register.emailRule')
    }
  ],
  tel: [
    {
      required: true,
      trigger: 'blur',
      message: '电话号码不正确'
    }
  ]
})

const loading = ref(false)
const store = useStore()
const onSubmit = () => {
  userRef.value.validate(vaild => {
    if (!vaild) return
    loading.value = true
    store.dispatch('user/register', user.value).then(() => {
      loading.value = false
    }).catch(err => {
      loading.value = false
      ElMessage.error(err.message)
    })
  })
}

const onCancel = () => {
  router.push('login')
}

</script>

<style lang="scss" scope>
$dark_gray:#889aa4;
.register-container {
  min-height: 100%;
  width: 100%;
  .register-form {
    position: relative;
    width: 90%;
    max-width: 100%;
    top: 50px;
    left: 40px;
  }
}

</style>
